package crmdna.interaction;

import static crmdna.common.AssertUtils.ensure;
import static crmdna.common.AssertUtils.ensureNotNull;
import static crmdna.common.OfyService.ofy;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import crmdna.common.APIException;
import crmdna.common.APIResponse.Status;
import crmdna.user.User;

public class InteractionCore {

	private final static long MAX_INTERACTION_SIZE = 900 * 1024;

	static InteractionProp create(String namespace, long memberId,
			String interaction, String interactionType, Date timestamp,
			long userId, String userEmail) {		

		ensureValid(timestamp, interactionType, interaction, userEmail);

		InteractionEntity interactionEntity = new InteractionEntity();
		interactionEntity.ns = timestamp.getTime() * 1000000
				+ new Random().nextInt(999999);
		interactionEntity.interaction = interaction;
		interactionEntity.interactionType = interactionType;
		interactionEntity.memberId = memberId;
		interactionEntity.userId = userId;
		interactionEntity.userEmail = userEmail;
				
		ofy(namespace).save().entity(interactionEntity).now();
		return interactionEntity.toProp();
	}

	private static void ensureValid(Date timestamp, String interactionType,
			String interaction, String login) {

		ensureNotNull(timestamp);
		ensureNotNull(interactionType);
		ensureNotNull(interaction);
		ensureNotNull(login);
		ensure(interaction.length() < MAX_INTERACTION_SIZE,
				"Interaction cannot have more than [" + MAX_INTERACTION_SIZE
						+ "] characters");
	}

//	static void delete(String client, long interactionId, String login) {
//
//		InteractionEntity interactionEntity = safeGet(client, interactionId,
//				login);
//		InteractionProp interactionProp = interactionEntity.toProp();
//
//		// need privilege to delete interaction created by someone else
//		long userId = UserCore.get(client, login).toProp().userId;
//
//		if (userId != interactionProp.userId)
//			crmdna.user.User.ensureAccess(client, login,
//					ResourceType.INTERACTION, 0, Action.WRITE);
//
//		ofy(client).delete().entity(interactionEntity).now();
//	}

	static InteractionEntity safeGet(String client, long interactionId) {		

		InteractionEntity interactionEntity = ofy(client).load()
				.type(InteractionEntity.class).id(interactionId).get();

		if (null == interactionEntity)
			throw new APIException(Status.ERROR_RESOURCE_NOT_FOUND,
					"Interaction id [" + interactionId + "] does not exist");

		return interactionEntity;
	}

	static List<InteractionProp> get_to_be_removed(String client,
			Iterable<Long> interactionIds, String login) {

		User.ensureValidUser(client, login);

		List<InteractionProp> props = new ArrayList<>();

		Map<Long, InteractionEntity> entities = ofy(client).load()
				.type(InteractionEntity.class).ids(interactionIds);

		for (InteractionEntity entity : entities.values()) {
			if (null != entity)
				props.add(entity.toProp());
		}
		
		return props;
	}	

//	static InteractionProp update(final String namespace,
//			final long interactionId, final String newInteraction,
//			final String newInteractionType, Date newTimestamp,
//			final long newUserId, final String newUserlogin) {
//
//		InteractionEntity interactionEntity = safeGet(namespace, interactionId);	
//		
//		if (null != newInteraction) 
//			interactionEntity.interaction = newInteraction;
//		
//		if (null != newInteractionType)
//			interactionEntity.interactionType = newInteractionType;
//		
////		if (null != newTimestamp) {
////			interactionEntity.ns = 
////		}
//
//		if (null == newTimestamp)
//			newTimestamp = new Date();
//
//		ensureValid(newInteraction, newInteractionType, newTimestamp);
//
//		final Date ts = newTimestamp;
//		interactionEntity = ofy(namespace).transact(new Work<InteractionProp>() {
//
//			@Override
//			public InteractionProp run() {
//				InteractionEntity interactionEntity = safeGet(namespace,
//						interactionId, newUserIdlogin);
//				if (null != newInteraction)
//					interactionEntity.interaction = newInteraction;
//
//				if (null != newInteractionType)
//					interactionEntity.interactionType = newInteractionType;
//
//				interactionEntity.timestamp = ts;
//				interactionEntity.userId = userId;
//
//				ofy(namespace).save().entity(interactionEntity).now();
//				return interactionEntity.toProp();
//			}
//		});
//
//		return interactionEntity;
//	}
}
